package com.zonski.jbots.wireless.client;

import com.zonski.jbots.engine.AbstractEngineRunner;
import com.zonski.jbots.game.JBotsGameController;
import com.zonski.jbots.game.entity.MinimalInputMind;

import javax.microedition.lcdui.game.GameCanvas;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 29, 2003
 * Time: 11:08:45 AM
 * To change this template use Options | File Templates.
 */
public class WirelessEngineRunner extends AbstractEngineRunner
{
    private JBotsGameController controller;
    private JBotsGameCanvas canvas;
    private MinimalInputMind playerMind;

    public WirelessEngineRunner(JBotsGameController controller, JBotsGameCanvas canvas, MinimalInputMind playerMind)
    {
        super(controller.getEngine());
        this.controller = controller;
        this.canvas = canvas;
        this.playerMind = playerMind;
    }

    public boolean checkIntro()
    {
        boolean result;
        if((this.canvas.getKeyStates() & (GameCanvas.FIRE_PRESSED | GameCanvas.GAME_A_PRESSED)) > 0)
        {
            this.controller.setMode(JBotsGameController.GAME_START);
            this.canvas.draw();
            result = false;
        }else{
            // TODO : we can set the start level here if we're keen
            this.canvas.draw();
            result = true;
        }
        return result;
    }

    protected final void onUpdate()
    {
        try
        {
            this.controller.update();
            this.canvas.draw();
            if(this.controller.getMode() == JBotsGameController.INTRO ||
                    this.controller.getMode() == JBotsGameController.GAME_OVER ||
                    this.controller.getMode() == JBotsGameController.DEMO)
            {
                if((this.canvas.getKeyStates() & (GameCanvas.FIRE_PRESSED | GameCanvas.GAME_A_PRESSED)) > 0)
                {
                    this.controller.setMode(JBotsGameController.GAME_START);
                }
            }else if(this.controller.getMode() == JBotsGameController.GAME){
                int keyStates = this.canvas.getKeyStates();
                this.playerMind.setDown(MinimalInputMind.EGG, ((keyStates & GameCanvas.FIRE_PRESSED) > 0));
                this.playerMind.setDown(MinimalInputMind.COMET,  ((keyStates & GameCanvas.GAME_B_PRESSED) > 0));
                this.playerMind.setDown(MinimalInputMind.BOMB, ((keyStates & GameCanvas.GAME_A_PRESSED)>0));
                this.playerMind.setDown(MinimalInputMind.SOUTH,  ((keyStates & GameCanvas.UP_PRESSED)>0));
                this.playerMind.setDown(MinimalInputMind.NORTH, ((keyStates & GameCanvas.DOWN_PRESSED)>0));
                this.playerMind.setDown(MinimalInputMind.WEST, ((keyStates & GameCanvas.LEFT_PRESSED)>0));
                this.playerMind.setDown(MinimalInputMind.EAST, ((keyStates & GameCanvas.RIGHT_PRESSED)>0));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
